
function startAnimate(){
	mui('.mui-scroll-wrapper').scroll();
	mui('#slider').slider({
		interval: 3000
	});
}
var viewApi = '';
function startView(){
	viewApi = mui('#app').view({
		defaultPage: '#myView'
	});

	var view = viewApi.view;
	(function(m) {
		//处理view的后退与webview后退
		var oldBack = m.back;
		m.back = function() {
			if (viewApi.canBack()) { //如果view可以后退，则执行view的后退
				viewApi.back();
			} else { //执行webview后退
				oldBack();
			}
		};
		view.addEventListener('pageBeforeShow', function(e) {
			console.log(e.detail.page.id + ' beforeShow');
			if(e.detail.page.id=='myView'){
				vm.type = 0;
				vm.curPage = 'list';
			}else if(e.detail.page.id=='fenlei'){
				vm.curPage = 'fenlei';
			}
		});
		view.addEventListener('pageShow', function(e) {
			console.log(e.detail.page.id + ' show');			
		});
		view.addEventListener('pageBeforeBack', function(e) {
			console.log(e.detail.page.id + ' beforeBack');
		});
		view.addEventListener('pageBack', function(e) {
			console.log(e.detail.page.id + ' back');
		});
	})(mui);
}

var vm = new Vue({
	el : '.main',
	mounted : function(){
		//startAnimate();
		startView();
		this.getSlider();
		this.getArea();
		this.historyData();

		//window.addEventListener('scroll', this.handleScroll);
	},
	data : {
		show : false,
		isSearch : false,
		searchKey : '',

		id : '',
		type : 0,
		typeTxt : '',
		area_id : '',
		order_type : 'create_time',
		curPage :'list',
		from : '',
		slider:[], 
		indicator : '',
		city : '',
		area : [],
		business : [],
		fenleiList : [],
		searchList : [],
		historyList : [],
		hotList:[],
		detailInfo : {},
		infoImg : [],
		infoImg_indicator : '',
		sliderUr: 'http://testhuodong.sparkingfuture.com/business-union/homelbt',
		areaUrl : 'http://testhuodong.sparkingfuture.com/business-union/get-areas',
		businessUrl : 'http://testhuodong.sparkingfuture.com/business-union/business',
		businessinfoUrl : 'http://testhuodong.sparkingfuture.com/business-union/business-info',
		searchUrl : 'http://testhuodong.sparkingfuture.com/business-union/search',
		historyUrl : 'http://testhuodong.sparkingfuture.com/business-union/search-history-hot',
		delsearchUrl : 'http://testhuodong.sparkingfuture.com/business-union/clear-history',


		page_size : 1,
		page_index_home : 1,
		page_index_fenlei : 1,
		showLoading : false
	},
	methods : {
		keyChange : function(){
			if( this.searchKey){
				this.isSearch = true;
			}else{
				this.isSearch = false;
			}
			// 请求模糊匹配
			this.tosearch(this.searchKey);
		},
		getSlider:function(){
			var _this = this;
			$.ajax({
				type : 'get',
				url : this.sliderUr,
				success:function(res){
					var json = JSON.parse(res);
					if( json.code == 0 ){
						_this.slider = json.data.data;
						if( _this.slider.length > 0 ){
							_this.indicator = json.data.data.length - 1;
						}
						//startAnimate();
					}
				}
			});
		},
		getArea: function(){
			var _this = this;
			$.ajax({
				type : 'get',
				url : this.areaUrl,
				success:function(res){
					var json = JSON.parse(res);
					if( json.code == 0 ){
						_this.city = json.data.city_data;
						_this.area = json.data.area_data;

						_this.area_id = json.data.city_data.id;
						_this.getBusiness(0);
					}
				}
			});
		},
		getBusiness:function(type){
			// debugger;
			var _this = this;
			var pageIndex;
			if(this.curPage == 'list'){
				pageIndex = this.page_index_home;
			}else if(this.curPage == 'fenlei'){
				pageIndex = this.page_index_fenlei;
			}
			getData(pageIndex,type).then(function(res){
				var json = JSON.parse(res);
				if( json.code == 0 ){
					if( json.data.data.length != 0 ){
						console.log('cauPage...',_this.curPage);
						if( _this.curPage == 'fenlei' ){
							_this.fenleiList = json.data.data;
						}else if(_this.curPage == 'list' ) {
							_this.business = json.data.data;
						}
					}else{
						//alert(1)
						console.log('m..',mui('#iscoll2').pullToRefresh())
						mui('#iscoll2').pullToRefresh().endPullUpToRefresh(true);
					}
				}
			}).catch(function(err){
				console.log('getBusiness..err..',err);
			});
		},
		sort:function(order_type){
			if(this.curPage == 'list'){
				this.page_index_home =  1;
				mui('#iscoll1').pullToRefresh().refresh(true)
			}else if(this.curPage == 'fenlei'){
				this.page_index_fenlei =  1;
				mui('#iscoll2').pullToRefresh().refresh(true);
			}
			this.order_type = order_type;
			this.getBusiness(this.type);
		},
		changeArea:function(id){
			this.area_id = id;
			this.getBusiness(0);
			this.show = false;
		},
		getDetail:function(){
			var _this = this;
			$.ajax({
				type : 'post',
				url : this.businessinfoUrl,
				data : {
					id : this.id,
					from : this.from
				},
				success:function(res){
					console.log(res);
					var json = JSON.parse(res);
					if( json.code == 0 ){
						_this.detailInfo = json.data; 	
						_this.infoImg = json.data.business_images;
						if( _this.infoImg.length > 0 ){
							_this.infoImg_indicator = json.data.business_images.length - 1;
						}
						//startAnimate();
					}
				}
			});
		},
		tosearch:function(val){
			var _this = this;
			$.ajax({
				type : 'post',
				url : this.searchUrl,
				data : {
					business_name : val
				},
				success:function(res){
					console.log(res);
					var json = JSON.parse(res);
					if( json.code == 0 ){
						_this.searchList = json.data;		
					}
				}
			});
		},
		tofenlei:function(type,typeTxt){
			var _this = this;
			this.fenleiList = [];
			this.page_index_fenlei = 1;
			mui('#iscoll2').pullToRefresh().refresh(true);
			this.type = type;
			this.typeTxt = typeTxt;
			viewApi.go('#fenlei');
			_this.getBusiness(_this.type);
		},
		todetail:function(id,from){
			this.id = id;
			this.from = from;
			this.getDetail();
			viewApi.go('#detail');
			this.searchKey = '';
			this.historyData();
		},
		historyData:function(){
			var _this = this;
			$.ajax({
				type : 'get',
				url : this.historyUrl,
				success:function(res){
					console.log(res);
					var json = JSON.parse(res);
					if( json.code == 0 ){
						_this.historyList = json.data.history;
						_this.hotList = json.data.hot;
					}
				}
			});
		},
		del:function(){
			var _this = this;
			$.ajax({
				type : 'get',
				url : this.delsearchUrl,
				success:function(res){
					var json = JSON.parse(res);
					if( json.code == 0 ){
						_this.historyList = [];
					}
				}
			});
		}
	},
	watch : {
		'searchKey' : 'keyChange'
	}	
});

mui.init();
(function(m) {
	m.ready(function() {
		m($('#iscoll1')).pullToRefresh({
			up: {
				height:100,
				callback: function() {
					var _this = this;
					console.log('this...',this)
					vm.page_index_home++;
					setTimeout(function(){
						getData(vm.page_index_home,vm.type).then(function(res){
							var json = JSON.parse(res);
							if( json.code == 0 ){
								if( json.data.data.length != 0 ){
									for(var i = 0; i<json.data.data.length;i++){
										vm.business.push(json.data.data[i]);
									}
									_this.endPullUpToRefresh(false);
								}else{							
									_this.endPullUpToRefresh(true);
								}
							}
						}).catch(function(xhr, type, errerThrown){
							mui.toast('网络异常,请稍候再试');
							_this.endPullupToRefresh(true);
						})
					},100);
				}
			}
		});
		m($('#iscoll2')).pullToRefresh({
			up: {
				height:100,
				callback: function() {
					var _this = this;
					vm.page_index_fenlei++;
					setTimeout(function(){
						getData(vm.page_index_fenlei,vm.type).then(function(res){
							var json = JSON.parse(res);
							if( json.code == 0 ){
								if( json.data.data.length != 0 ){
									for(var i = 0; i<json.data.data.length;i++){
										vm.fenleiList.push(json.data.data[i]);
									}
									_this.endPullUpToRefresh(false);
								}else{
									_this.endPullUpToRefresh(true);
								}
							}
						}).catch(function(xhr, type, errerThrown){
							mui.toast('网络异常,请稍候再试');
							_this.endPullupToRefresh(true);
						},100);
					})
				}
			}
		});
	});
})(mui);

function getData(idx,type) {
	return new Promise(function(resolve, reject){
		var	data = {
			type : type,
			area_id : vm.area_id,
			order_type : vm.order_type,
			page_size : vm.page_size,
			page_index : idx
		}
		$.ajax({
			url : vm.businessUrl,
			data:data,
			type: 'POST',
			success: function(res) {
				resolve(res)
			},
			error: function(xhr, type, errerThrown) {
				reject(xhr, type, errerThrown);
			}
		});
	});
}